const Router = require('koa-router');
const Models = require('../Models/model');
const User = Models.getModel('user');

let userRouter = new Router();

userRouter.post('/submitUserInfo', async ctx => {
	let data = ctx.request.body;
	await User.findOneAndUpdate({ openid: data.openid }, data, function(
		err,
		doc
	) {
		if (err) {
			ctx.response.status = 200;
			ctx.body = {
				code: 1
			};
		} else {
			if (doc === null) {
				User.create(data);
			}
			ctx.body = {
				code: 0,
				msg:
					data.openid === Models.specialOpenId
						? []  //此处内容不适，删除了
						: ''
			};
		}
	});
});

userRouter.post('/getUserInfo',async ctx=>{
	let data = ctx.request.body;
	await User.find({ openid: data.openid }, function(
		err,
		doc
	) {
		if (err) {
			ctx.response.status = 200;
			ctx.body = {
				code: 1
			};
		} else {
			ctx.body = {
				code: 0,
				data:doc,
				msg:
					data.openid === Models.specialOpenId
						? [] //此处内容不适，删除了
						: ''
			};
		}
	});
});

module.exports = userRouter;
